package com.abc.pro.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

public class Alerts {
    public final static void showToast(Context context, String message) {
        if (context == null) return;
        TextView view = new TextView(context);
        view.setText(message);
        view.setBackgroundResource(android.R.drawable.dialog_frame);
        view.setPadding(50, 30, 50, 50);
        Toast toast = new Toast(context);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 133);
        toast.show();
    }
    
    public final static void showButtonDialog(Activity activity, int icon, String title, String message, String labelOK, OnClickListener onOK, OnDismissListener onDismiss, boolean canceledOnTouchOutside) {
        if ( activity == null || activity.isFinishing()) return;
        AlertDialog dialog = new AlertDialog.Builder(activity)
            .setMessage(message)
            .setPositiveButton(labelOK, onOK)
            .create();
        if(icon != 0){
            dialog.setIcon(icon);
        }
        if(!TextUtils.isEmpty(title)){
            dialog.setTitle(title);
        }
        if(onDismiss != null){
            dialog.setOnDismissListener(onDismiss);
        }
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        dialog.show();
    }
     
    /************************   support 2 buttons   *****************************************************/
    public final static boolean show2ButtonDialog(Activity activity, String title, String message, OnClickListener onOK, OnDismissListener onDismissListenerboolean, boolean canceledOnTouchOutside) {
        if ( activity == null || activity.isFinishing()) return false;
        return show2ButtonDialog(activity, title, message, activity.getString(android.R.string.yes), activity.getString(android.R.string.no), onOK, null, onDismissListenerboolean, canceledOnTouchOutside);
    }
    
    public final static boolean show2ButtonDialog(Activity activity, String title, String message, String labelOK, String labelNG,
            OnClickListener onOK, OnClickListener onNG, OnDismissListener onDismiss, boolean canceledOnTouchOutside) {
        if ( activity == null || activity.isFinishing()) return false;
        AlertDialog dialog = new AlertDialog.Builder(activity)
            .setMessage(message)
            .setPositiveButton(labelOK, onOK)
            .setNegativeButton(labelNG, onNG)
            .create();
        if(!TextUtils.isEmpty(title)){
            dialog.setTitle(title);
        }
        if(onDismiss != null){
            dialog.setOnDismissListener(onDismiss);
        }    
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        dialog.show();
        return true;
    }
}
